<template>
  <div>
    <el-card style="height: 60px">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" style="margin-top: -10px; margin-left: 10px" ref="ruleForm">
        <el-form-item label="账号查询" prop="rentalAccount">
          <el-input v-model="searchForm.rentalAccount" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" icon="el-icon-search">查询</el-button>
          <el-button type="danger" @click="resetForm('ruleForm')" icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-form style="margin-bottom: -20px; margin-top: -15px; margin-left: 10px">
        <el-form-item style="margin-bottom: 10px">
          <el-button type="success" @click="dialogVisible = true; title = '新增信息'; resetFormData()" size="small" style="height: 30px; width: 70px; margin-top: 10px" icon="el-icon-plus">新增</el-button>
          <!--添加信息的弹出框-->
          <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="550px"
            height="200px"
          >
            <el-form :model="editForm" label-width="100px" label-position="left"  ref="ruleForm">
              <el-form-item label="用户名" style="margin-bottom: 10px">
                <el-input v-model="editForm.role" placeholder="请输入用户名"></el-input>
              </el-form-item>

              <el-form-item label="租房账号" style="margin-bottom: 10px">
                <el-input v-model="editForm.rentalAccount" placeholder="请输入租房账号"></el-input>
              </el-form-item>

              <el-form-item label="租房名称" style="margin-bottom: 10px">
                <el-input v-model="editForm.rentalName" placeholder="请输入租房名称"></el-input>
              </el-form-item>

              <el-form-item label="价格" style="margin-bottom: 10px">
                <el-input v-model="editForm.price" placeholder="请输入价格">
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>

              <el-form-item label="电话" style="margin-bottom: 10px">
                <el-input v-model="editForm.phone" placeholder="请输入电话"></el-input>
              </el-form-item>

              <el-form-item label="邮箱" style="margin-bottom: 10px">
                <el-input v-model="editForm.email" placeholder="请输入邮箱"></el-input>
              </el-form-item>

              <el-form-item label="官网" style="margin-bottom: 10px">
                <el-input v-model="editForm.officialWebsite" placeholder="请输入官网URL"></el-input>
              </el-form-item>

              <el-form-item label="状态" style="margin-bottom: 10px">
                <el-select v-model="editForm.status" placeholder="请选择状态" style="width: 100%">
                  <el-option label="审核中" value="pending"></el-option>
                  <el-option label="审核通过" value="published"></el-option>
                  <el-option label="审核失败" value="offline"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="介绍" style="margin-bottom: 10px">
                <el-input
                  type="textarea"
                  v-model="editForm.introduction"
                  placeholder="请输入介绍"
                  :rows="3"
                ></el-input>
              </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
            </span>
          </el-dialog>
          <el-button type="danger" @click="handleDelete1" size="small" style="height: 30px; width: 70px;margin-left: 10px" icon="el-icon-delete">删除</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-table
        @selection-change="handleSelectionChange"
                :data="tableData"
        v-loading="loading"
        element-loading-text="拼命加载中"

        style="width: 100%">
        <el-table-column
          type="selection"
          width="55"
        >
        </el-table-column>
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="租房图片">
          <template #default="{ row }">
            <img :src="row.rentalPicture"  style="max-width: 50px; max-height: 50px">
          </template>
        </el-table-column>
        <el-table-column label="房东账号" prop="rentalAccount"></el-table-column>
        <el-table-column label="租房名称" prop="rentalName"></el-table-column>
        <el-table-column label="价格" prop="price"></el-table-column>
        <el-table-column label="电话" prop="phone"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="官网" prop="officialWebsite"></el-table-column>
        <el-table-column label="介绍" prop="introduction"></el-table-column>
        <el-table-column label="角色" prop="role"></el-table-column>
        <el-table-column label="状态" prop="status"></el-table-column>
        <el-table-column label="操作" width="160" style="margin-right: 30px"> <!-- 设置固定宽度确保足够空间 -->
          <template #default="{ row }">
            <div style="display: flex; align-items: center; justify-content: space-between; width: 100%">
              <el-button
                size="mini"
                @click="handleEdit(row)"
                style="margin-right: 10px"> <!-- 使用margin-right替代margin-left -->
                编辑
              </el-button>
              <el-button
                v-if="row.parentTypeId!== 0"
                size="mini"
                type="danger"
                style="margin-right: 70px"
                @click="handleDelete(row)">
                删除
              </el-button>
            </div>
          </template>
          <template #empty>
            <el-empty description="暂无数据"></el-empty>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; align-items: center; margin-top: 30px; margin-left: 400px;">
        <el-pagination background @size-change="handleSizeChange" :page-sizes="[3, 6, 9]"
                       @current-change="handleCurrentChange" :current-page="searchForm.pageNum" :page-size="searchForm.pageSize"
                       layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <el-button style="margin-left: 10px" size="small" type="primary" plain @click="toPage">跳转</el-button>
      </div>
    </el-card>
  </div>
</template>


<script>
import { queryLandlordInfo, submitForm, updateForm, deleteForm, batchDeleteForm } from '@/api/md/Landlord.js'

export default {
  name: 'Landlord',
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      title: '新增信息',
      loading: false,
      searchForm: { //专门用于搜索的表单
        rentalAccount: '',
        id: '',
        price: '',
        phone: '',
        email: '',
        rentalName: '',
        officialWebsite: '',
        status: '',
        role: '',
        introduction: '',
        pageNum: 1,
        pageSize: 6
      },
      editForm: { //专门用于编辑的表单
        id: '',
        pageNum: 1,
        pageSize: 6,
        price: '',
        phone: '',
        email: '',
        rentalName: '',
        officialWebsite: '',
        status: '',
        role: '',
        rentalAccount: '',
        introduction: ''
      },
      total: 0,
    }
  },
  methods: {
    onSubmit() {
      console.log("输入的账号:", this.searchForm.rentalAccount); // 明确打印值
      // 这里添加查询逻辑，例如根据账号查询
      this.searchForm.pageNum = 1
      this.searchForm.pageSize = 3
      this.queryLandlordInfo();
    },
    // 查询房东所有信息
    queryLandlordInfo() {
      this.loading = true;
      queryLandlordInfo(this.searchForm)
        .then(ret => {
          this.tableData = ret.data.list
          this.total = ret.data.total//更新总条数
          this.$message({
            type: 'success',
            message: '查询成功!'
          });
          this.pageSize = 3
          this.pageNum = 1
        })
        .catch(err => {
          this.$message({
            message: '查询失败',
            type: 'error'
          });
        })
    .finally(() => {
        this.loading = false;
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.searchForm.rentalAccount = ''
      this.searchForm.pageNum = 1
      this.queryLandlordInfo();
    },
    handleEdit(row) {
      this.title = '编辑信息';
      this.dialogVisible = true;
      this.editForm = JSON.parse(JSON.stringify(row)); // 深拷贝避免引用问题
    },
    toPage() {
      this.queryLandlordInfo()
    },
    handleDelete(row) {
      // 实际应用中需对接后端删除接口
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteForm(row.id).then(ret => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.queryLandlordInfo();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //批量删除
    handleDelete1() {
      const ids = this.multipleSelection;
      if (ids.length === 0) {
        this.$message({
          type: 'info',
          message: '请选择要删除的数据!'
        });
        return;
      }
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDeleteForm(ids).then(ret => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.queryLandlordInfo();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 重置编辑表单
    resetFormData() {
      this.editForm = {
        id: '',
        role: '',
        rentalAccount: '',
        rentalName: '',
        price: '',
        phone: '',
        email: '',
        officialWebsite: '',
        status: '',
        introduction: ''
      };
    },
    // 选中事件
    handleSelectionChange(val) {
      this.multipleSelection = val.map(item => item.id);
    },
    submitForm(formName) {
      //如果是新增，则执行新增逻辑，否则执行编辑逻辑
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === "新增信息") {
            submitForm(this.editForm).then(ret => {
              this.$message({
                type: 'success',
                message: '添加成功!',
              });
              this.searchForm.pageNum = 1
              this.$refs[formName].resetFields()
              this.dialogVisible = false;
              this.queryLandlordInfo();
            });
          } else {
            // 修改此处，调用正确的方法
            updateForm(this.editForm).then(ret => {
              this.$message({
                type: 'success',
                message: '修改成功!',
              });
              this.dialogVisible = false;
              this.searchForm.pageNum = 1
              this.$refs[formName].resetFields()
              this.queryLandlordInfo();
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`);
      this.searchForm.pageSize = val;
      this.searchForm.pageNum = 1
      this.queryLandlordInfo()
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`);
      this.searchForm.pageNum = val
      this.queryLandlordInfo()
    },
  },
  created() {
    this.queryLandlordInfo();
  }
}
</script>

<style scoped lang="scss">
</style>
